import requests
from lxml import etree
from ddddocr import DdddOcr
from util.timestamp import get_timestamp
ocr = DdddOcr(show_ad=False)

res_login_get = requests.get("https://www.guwendao.net/user/login.aspx?from=http://www.guwendao.net/user/collect.aspx")

tree = etree.HTML(res_login_get.text)
__VIEWSTATE = tree.xpath('//input[@id="__VIEWSTATE"]/@value')[0]
__VIEWSTATEGENERATOR = tree.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')[0]

while True:
    res_code = requests.get(f"https://www.guwendao.net/RandCode.ashx?t={get_timestamp()}")
    # print(res_code.cookies)
    # with open(f'./static/yzm/古诗文网.jpg', 'wb') as f:
    #     f.write(res_code.content)

    r = ocr.classification(res_code.content)
    res_login_post = requests.post("https://www.guwendao.net/user/login.aspx?from=http://www.guwendao.net/user/collect.aspx",data={
        "__VIEWSTATE": __VIEWSTATE,
        "__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,
        "email": "1741964482@qq.com",
        "pwd": "d1741964482",
        "code": r,
        "denglu": "登录",
    }, cookies=res_code.cookies)
    if "您输入的验证码有误" in res_login_post.text:
        print("验证码错误")
    else:
        break

res_collect = requests.get("https://www.guwendao.net/user/collect.aspx",cookies=res_login_post.cookies)
tree = etree.HTML(res_collect.text)
items = tree.xpath('//div[@class="cont"]/a/text()')
for item in items:
    print(item)